using System.Windows;
using System.Windows.Input;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Microsoft.Data.SqlClient;

namespace RugbyClub.ViewModels
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private string _username;
        private string _password;
        private bool _isPasswordPlaceholderVisible = true;

        public string Username
        {
            get => _username;
            set
            {
                _username = value;
                OnPropertyChanged();
            }
        }

        public string Password
        {
            get => _password;
            set
            {
                _password = value;
                IsPasswordPlaceholderVisible = string.IsNullOrEmpty(_password);
                OnPropertyChanged();
            }
        }

        public bool IsPasswordPlaceholderVisible
        {
            get => _isPasswordPlaceholderVisible;
            set
            {
                _isPasswordPlaceholderVisible = value;
                OnPropertyChanged();
            }
        }

        public ICommand LoginCommand { get; }

        public MainWindowViewModel()
        {
            LoginCommand = new RelayCommand(Login);
        }

        private void Login()
        {
            // Authenticate user
            string role = AuthenticateUser(Username, Password);

            if (role == "Coach")
            {
                // Navigate to Coach view
                Application.Current.Dispatcher.Invoke(() =>
                {
                    CoachWindow coachWindow = new CoachWindow();
                    coachWindow.Show();
                    Application.Current.MainWindow.Close();
                });
            }
            else if (role == "Player")
            {
                // Navigate to Player view
                Application.Current.Dispatcher.Invoke(() =>
                {
                    PlayerWindow playerWindow = new PlayerWindow();
                    playerWindow.Show();
                    Application.Current.MainWindow.Close();
                });
            }
            else if (role == "President")
            {
                // Navigate to President view
                Application.Current.Dispatcher.Invoke(() =>
                {
                    PresidentWindow presidentWindow = new PresidentWindow();
                    presidentWindow.Show();
                    Application.Current.MainWindow.Close();
                });
            }
            else
            {
                MessageBox.Show("Invalid username or password");
            }
        }

        private string AuthenticateUser(string username, string password)
        {
            // Implement your SQL Server authentication logic here
            // This is a placeholder implementation
            if (username == "coach" && password == "password")
            {
                return "Coach";
            }
            else if (username == "player" && password == "password")
            {
                return "Player";
            }
            else if (username == "president" && password == "password")
            {
                return "President";
            }
            else
            {
                return string.Empty;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

